﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

using Common;
using Common.DbConstants;
using Common.FileManagement;
using BusinessEntity.Common;
using BusinessEntity.User;
using BusinessEntity.FileManagement;

using Microsoft.ApplicationBlocks.Data;

namespace DataAccess.Communication
{
    public class FileAdapter
    {
        #region PROPERTIES
        public string ConnectionString
        {
            get
            {
                return Utility.GetApplicationConnectionString();
            }
        }
        #endregion


        public static SortableCollection<MyFile> SearchFile(string sessionId, MyFile searchMyFile, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            MyFile oMyFile = null;
            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<MyFile> oMyFiles = new SortableCollection<MyFile>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                                        Utility.GetApplicationConnectionString(),
                                                        CommandType.StoredProcedure,
                                                        DbStoredProceduresFileManagement.FILEMANAGEMENT_SEARCHFILE,
                                                        new SqlParameter(DbParameters.SESSIONID, sessionId),
                                                         new SqlParameter(DbParameters.PATIENTID, searchMyFile.PatientId),
                                                        outputSuccessMessage,
                                                        outputErrorMessage
                                                    );

                while (dataReader.Read())
                {
                    oMyFile = new MyFile();
                    oMyFile.Id = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.FILEID);
                    oMyFile.FileContent = (byte[])dataReader[DbColumnNameFileManagement.FILECONTENT];
                    oMyFile.Note = dataReader[DbColumnNameFileManagement.NOTE].ToString();
                    oMyFile.FileName = dataReader[DbColumnNameFileManagement.FILENAME].ToString();
                    oMyFile.DocumentTypeId = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.DOCUMENTTYPEID);
                    oMyFile.NotifyTo = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.UserId);
                    oMyFile.SourceFileFullPath  = dataReader[DbColumnNameFileManagement.ORIGINALFILEPATH].ToString();
                    oMyFile.CreatedBy = dataReader[DbColumnNameFileManagement.FILECREATEDBY].ToString();
                    oMyFile.CreatedOn = (DateTime)dataReader[DbColumnNameFileManagement.FILECREATEDON];                  
                    oMyFiles.Add(oMyFile);
                }

                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oMyFiles;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }

        public static void UploadFile(string sessionId, MyFile oFile, out string successMessage, out string errorMessage)
        {
            errorMessage = string.Empty;
            successMessage = string.Empty;
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();
                SqlParameter outputIdentity = DbUtility.GetIdentityParameter();

                SqlHelper.ExecuteNonQuery(
                    Utility.GetApplicationConnectionString(),
                    CommandType.StoredProcedure,
                    DbStoredProceduresFileManagement.FILEMANAGEMENT_UPDATEFILE,
                    new SqlParameter(DbParameters.SESSIONID, sessionId),
                    new SqlParameter(DbFileParameters.PRID, oFile.Id),
                    new SqlParameter(DbFileParameters.PRPATIENTID, oFile.PatientId),
                    new SqlParameter(DbFileParameters.PRORIGINLAFILENAME, oFile.SourceFileName),
                    new SqlParameter(DbFileParameters.PRORIGINALFILEPATH, oFile.SourceFileFullPath),
                    new SqlParameter(DbFileParameters.PRFILENAME, oFile.FileName),
                    new SqlParameter(DbFileParameters.PRFILETYPEID, oFile.FileTypeId),
                    new SqlParameter(DbFileParameters.PRDOCTYPEID, oFile.DocumentTypeId),                    
                    new SqlParameter(DbFileParameters.PRRECORDID, 1),
                    new SqlParameter(DbFileParameters.PRCONTENTID, oFile.FileContent),
                    new SqlParameter(DbFileParameters.PRNOTE, oFile.Note),
                    new SqlParameter(DbFileParameters.PRNOTIFYTO, oFile.NotifyTo),
                    outputIdentity,
                    outputSuccessMessage,
                    outputErrorMessage
                );     

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }

                if (outputIdentity.Value != null && oFile.Id == null)
                {
                    oFile.Id = Convert.ToInt32(outputIdentity.Value.ToString());
                }

            }
            catch (SqlException ex)
            {
                errorMessage = Utility.GetExceptionMessageString(ex);
            }
        }

        public static SortableCollection<MyFile> GetFile(string sessionId, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            MyFile oMyFile = null;
            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<MyFile> oMyFiles = new SortableCollection<MyFile>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                                        Utility.GetApplicationConnectionString(),
                                                        CommandType.StoredProcedure,
                                                        DbStoredProceduresFileManagement.FILEMANAGEMENT_GETFILE,
                                                        new SqlParameter(DbParameters.SESSIONID, sessionId),
                                                        outputSuccessMessage,
                                                        outputErrorMessage
                                                    );

                while (dataReader.Read())
                {
                    oMyFile = new MyFile();
                    oMyFile.Id = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.FILEID);
                    oMyFile.FileName = dataReader[DbColumnNameFileManagement.FILENAME].ToString();
                    oMyFile.FileContent = (byte[])dataReader[DbColumnNameFileManagement.FILECONTENT];
                    oMyFiles.Add(oMyFile);
                }

                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oMyFiles;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }

        public static void DeleteFile(string sessionId, int fileId, out string successMessage, out string errorMessage)
        {
            errorMessage = string.Empty;
            successMessage = string.Empty;
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();
                
                SqlHelper.ExecuteNonQuery(
                    Utility.GetApplicationConnectionString(),
                    CommandType.StoredProcedure,
                    DbStoredProceduresFileManagement.FILEMANAGEMENT_DELETEFILE,
                    new SqlParameter(DbParameters.SESSIONID, sessionId),
                    new SqlParameter(DbFileParameters.PRID, fileId),
                    
                    outputSuccessMessage,
                    outputErrorMessage
                );

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }

                
            }
            catch (SqlException ex)
            {
                errorMessage = Utility.GetExceptionMessageString(ex);
            }
        }

        public static SortableCollection<MyFileType> GetFileType(string sessionId, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            MyFileType oMyFileType = null;
            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<MyFileType> oMyFileTypes = new SortableCollection<MyFileType>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                                        Utility.GetApplicationConnectionString(),
                                                        CommandType.StoredProcedure,
                                                        DbStoredProceduresFileManagement.FILEMANAGEMENT_GETFILETYPE,
                                                        new SqlParameter(DbParameters.SESSIONID, sessionId),
                                                        outputSuccessMessage,
                                                        outputErrorMessage
                                                    );

                while (dataReader.Read())
                {
                    oMyFileType = new MyFileType();
                    oMyFileType.Id = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.FILEID);
                    oMyFileType.Name = dataReader[DbColumnNameFileManagement.FILETYPENAME].ToString();
                    oMyFileType.DisplayOrder = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.DISPLAYORDER);// (int)dataReader["DisplayOrder"];
                    oMyFileType.MimeType = dataReader[DbColumnNameFileManagement.MIMETYPE].ToString();
                    oMyFileTypes.Add(oMyFileType);
                }

                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oMyFileTypes;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }

        public static SortableCollection<DocumentType> GetDocumentType(string sessionId, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            DocumentType oDocumentType = null;
            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<DocumentType> oDocumentTypes = new SortableCollection<DocumentType>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                                        Utility.GetApplicationConnectionString(),
                                                        CommandType.StoredProcedure,
                                                        DbStoredProceduresFileManagement.FILEMANAGEMENT_GETDOCTYPE,
                                                        new SqlParameter(DbParameters.SESSIONID, sessionId),
                                                        outputSuccessMessage,
                                                        outputErrorMessage
                                                    );

                while (dataReader.Read())
                {
                    oDocumentType = new DocumentType();
                    oDocumentType.Id = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.FILEID);
                    oDocumentType.Name = dataReader[DbColumnNameFileManagement.FILETYPENAME].ToString();
                    oDocumentType.DisplayOrder = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.DISPLAYORDER);// (int)dataReader["DisplayOrder"];

                    oDocumentTypes.Add(oDocumentType);
                }

                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oDocumentTypes;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }

        public static SortableCollection<CoreUser> GetCoreUsers(string sessionId, out string successMessage, out string errorMessage)
        {
            IDataReader dataReader = null;
            CoreUser oCoreUser = null;
            errorMessage = string.Empty;
            successMessage = string.Empty;

            SortableCollection<CoreUser> oCoreUsers = new SortableCollection<CoreUser>();
            try
            {
                SqlParameter outputSuccessMessage = DbUtility.GetSuccessMessageParameter();
                SqlParameter outputErrorMessage = DbUtility.GetErrorMessageParameter();


                dataReader = SqlHelper.ExecuteReader(
                                                        Utility.GetApplicationConnectionString(),
                                                        CommandType.StoredProcedure,
                                                        DbStoredProceduresFileManagement.FILEMANAGEMENT_GETCOREUSER,
                                                        new SqlParameter(DbParameters.SESSIONID, sessionId),
                                                        outputSuccessMessage,
                                                        outputErrorMessage
                                                    );

                while (dataReader.Read())
                {
                    oCoreUser = new CoreUser();
                    oCoreUser.Id = Utility.GetDataReaderIntegerFieldValue(dataReader, DbColumnNameFileManagement.USERID);
                    oCoreUser.LoginId = dataReader[DbColumnNameFileManagement.LOGINID].ToString();
                    oCoreUser.City = dataReader[DbColumnNameFileManagement.CITY].ToString();
                    oCoreUsers.Add(oCoreUser);
                }

                dataReader.Close();

                if (outputSuccessMessage.Value != null)
                {
                    successMessage = outputSuccessMessage.Value.ToString();
                }

                if (outputErrorMessage.Value != null)
                {
                    errorMessage = outputErrorMessage.Value.ToString();
                }
                return oCoreUsers;
            }
            catch (Exception ex)
            {
                errorMessage = errorMessage + Environment.NewLine + Utility.GetExceptionMessageString(ex);
                return null;
            }
            finally
            {
                if (dataReader != null)
                {
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
            }
        }
    }
}